package com.yfp.device.service.impl;

import com.yfp.device.domain.*;
import com.yfp.device.domain.dto.ApkVersionResponse;
import com.yfp.device.domain.dto.MachineApkFeedbackRequest;
import com.yfp.device.mapper.ApkVersionMapper;
import com.yfp.device.mapper.MachineApkMapper;
import com.yfp.device.mapper.MachineMapper;
import com.yfp.device.mapper.VersionPushRecordMapper;
import com.yfp.device.service.ApkVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * apk版本服务
 */
@Service
@Slf4j
public class ApkVersionServiceImpl implements ApkVersionService {

    /**
     * apk版本MAPPER
     */
    private final ApkVersionMapper apkVersionMapper;

    private final MachineApkMapper machineApkMapper;

    private final MachineMapper machineMapper;

    private final VersionPushRecordMapper versionPushRecordMapper;

    public ApkVersionServiceImpl(ApkVersionMapper apkVersionMapper, MachineApkMapper machineApkMapper,
                                 VersionPushRecordMapper versionPushRecordMapper, MachineMapper machineMapper) {
        this.apkVersionMapper = apkVersionMapper;
        this.machineApkMapper = machineApkMapper;
        this.versionPushRecordMapper = versionPushRecordMapper;
        this.machineMapper = machineMapper;
    }

    /**
     * 获取最新版本apk
     *
     * @param machineSn 机器编号
     * @return 最新版本apk
     */
    @Override
    public R<ApkVersionResponse> getLeastApkVersion(String machineSn) {
        try {
            ApkVersionResponse apkVersionResponse = new ApkVersionResponse();
            ApkVersion apkVersion = apkVersionMapper.selectByMachineSn(machineSn);
            if (apkVersion != null) {
                apkVersionResponse.setMachineSn(machineSn);
                apkVersionResponse.setVersionName(apkVersion.getVersionName());
                apkVersionResponse.setApkUrl(apkVersion.getUrl());
                apkVersionResponse.setSize(apkVersion.getSize());
                apkVersionResponse.setVersionNo(apkVersion.getVersionNo());
                apkVersionResponse.setInnerVersionNo(apkVersion.getInnerVersionNo());
                return R.ok(apkVersionResponse);
            } else {
                log.error("最新版本不存在");
                return R.fail("最新版本不存在");
            }
        } catch (Exception e) {
            log.error("获取最新版本apk失败", e);
            return R.fail();
        }
    }

    /**
     * 机器版本反馈
     *
     * @param request 请求体
     * @return 响应体
     */
    public R<?> machineApkFeedback(MachineApkFeedbackRequest request) {
        try {
            if (request.getPushRecordNo() != null) {
                VersionPushRecord versionPushRecord = new VersionPushRecord();
                versionPushRecord.setRecordNo(request.getPushRecordNo());
                versionPushRecord.setUpgradeStatus(1);
                versionPushRecordMapper.updateByPrimaryKey(versionPushRecord);
            }
            MachineApk machineApk = machineApkMapper.selectByPrimaryKey(request.getMachineSn());
            MachineApk newMachineApk = new MachineApk();
            newMachineApk.setMachineId(request.getMachineSn());
            newMachineApk.setInnerVersionNo(request.getInnerVersionNo());
            newMachineApk.setVersionNo(request.getVersionNo());
            newMachineApk.setVersionName(request.getVersionName());
            newMachineApk.setUpdateTime(new Date());
            if (machineApk == null) {
                newMachineApk.setCreateTime(new Date());
                machineApkMapper.insertSelective(newMachineApk);
            } else {
                machineApkMapper.updateByPrimaryKeySelective(newMachineApk);
            }
            Machine machine = new Machine();
            machine.setMachineId(request.getMachineSn());
            machine.setFirmwareVersion(request.getFirmwareVersion());
            machineMapper.updateByPrimaryKeySelective(machine);
            return R.ok();
        } catch (Exception e) {
            log.error("机器版本反馈失败", e);
            return R.fail("机器版本反馈失败");
        }
    }
}
